import { createRouter, createWebHashHistory } from 'vue-router'
import store from "@/store";
const routes = [
  {
    path: '/index',
    redirect: "/index/overview",
    component: () => import("@/views/index.vue"),
    children: [
      {
        path: 'overview',
        name: 'Overview',
        component: () => import("@/views/overview/Overview.vue"),
      },
      {
        path: 'carlist',
        name: 'CarList',
        component: () => import("@/views/detail/CarList"),
      },
      {
        path: 'detail',
        redirect: 'detail/carDetail',
        name: 'Detail',
        component: () => import("@/views/detail/Detail"),
        children: [
          {
            path: 'carDetail',
            name: 'CarDetail',
            component: () => import("@/views/detail/details/CarDetail"),
          },
          {
            path: 'service',
            name: 'Service',
            component: () => import("@/views/detail/details/Service"),
          },
          {
            path: 'function',
            name: 'Function',
            component: () => import("@/views/detail/details/Function"),
          },
          {
            path: 'new',
            name: 'New',
            component: () => import("@/views/detail/details/New"),
          },
          {
            path: 'arguments',
            name: 'Arguments',
            component: () => import("@/views/detail/details/Arguments"),
          },
          {
            path: 'defect',
            name: 'Defect',
            component: () => import("@/views/detail/details/Defect"),
          },
          {
            path: 'data',
            name: 'Data',
            component: () => import("@/views/detail/details/Data"),
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import("@/views/Login.vue"),
  },
  {
    path: '/',
    redirect: 'Login',
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach(
  (to, from, next) => {
    if (!localStorage.token  && to.name != 'Login') {
      next({ name: 'Login' })
    } else {
      next()
    }
  }
)

export default router
